package ordersizecolordistribute

import (
	// "net/http"
	"encoding/json"
	"strconv"
	"time"
	// "github.com/dgrijalva/jwt-go"
	"github.com/labstack/echo"
	"gopkg.in/mgo.v2/bson"
	// "github.com/jinzhu/gorm"

	"oxy_server/model"
	"oxy_server/module/log"
	"oxy_server/until"
)

type (
	SizeColor struct {
		ID             bson.ObjectId `json:"_id" bson:"_id"`
		ContractNumber string        `json:"contractnumber" bson:"contractnumber"` //合同编号
		SupplierName   string        `json:"suppliername" bson:"suppliername"`     //供应商
		Supplier       string        `json:"supplier" bson:"supplier"`             //供应商
		OrderMoney     string        `json:"ordermoney" bson:"ordermoney"`         //下单金额
		OrderTime      time.Time     `json:"ordertime" bson:"ordertime"`           //下单时间
		PaymentPlan    string        `json:"paymentplan" bson:"paymentplan"`       //付款计划
		Evidence       string        `json:"evidence" bson:"evidence"`             //凭证
		Note           string        `json:"note" bson:"note"`                     //备注
	}
	insertSizeColor struct {
		ContractNumber string    `json:"contractnumber" bson:"contractnumber"` //合同编号
		SupplierName   string    `json:"suppliername" bson:"suppliername"`     //供应商
		Supplier       string    `json:"supplier" bson:"supplier"`             //供应商
		OrderMoney     string    `json:"ordermoney" bson:"ordermoney"`         //下单金额
		OrderTime      time.Time `json:"ordertime" bson:"ordertime"`           //下单时间
		PaymentPlan    string    `json:"paymentplan" bson:"paymentplan"`       //付款计划
		Evidence       string    `json:"evidence" bson:"evidence"`             //凭证
		Note           string    `json:"note" bson:"note"`                     //备注
	}
)

type OXYSizeColorData struct{}

func NewOXYData() *OXYSizeColorData {
	Data := &OXYSizeColorData{}
	return Data
}

func (opd *OXYSizeColorData) GetPaymentPlanListHandler(c echo.Context) error {
	currentpage, err := strconv.Atoi(c.QueryParam("currentpage"))
	perpage, err := strconv.Atoi(c.QueryParam("perpage"))
	jsonquery := c.QueryParam("like")
	if err != nil {
		log.Error("解析参数错误", err)
		c.JSON(403, map[string]interface{}{
			"msg": "error",
		})
		return err
	}
	model.DB().Dbname = "OxyCms"
	model.DB().Document = "payment_plan"
	M, err := until.JsonToBson(jsonquery)
	if err != nil {
		log.Error("json-> bson 失败", err)
		c.JSON(403, map[string]interface{}{
			"msg": "error",
		})
		return err
	}
	query, count, err := model.DB().Query(M, currentpage, perpage)
	if err != nil {
		log.Error("查询出错", err)
		c.JSON(404, map[string]interface{}{
			"msg": "error",
		})
		return err
	}
	ps := []paymentplan{}
	query.All(&ps)

	str, err := json.Marshal(ps)
	if err != nil {
		log.Error("解析参数错误", err)
		c.JSON(403, map[string]interface{}{
			"msg": "error",
		})
		return err
	}
	c.JSON(200, map[string]interface{}{
		"msg":   "success",
		"data":  string(str),
		"count": count,
	})

	return nil
}
